import { getComments, getCaseDetail, praise, unpraise, saveComment } from '../../../../../api/weddingMan/index';
import { getStorageItem, setStorageItem } from '../../../../../utils/storage';
const app = getApp();

Page({
  data: {
    caseId: '',
    case_name: '',
    more_information_json: '',
    imageList: [],
    videoList: [],
    commentCt: '',
    commentList: [],
    likes: 0,
    isLike: false,
    avator: '',
    userId: '',
    phone: '',
    name: '',
    isFocus: false,
  },
  onLoad: function (options) {
    this.setData({
      caseId: options.caseId,
      userId: options.userId,
      phone: options.phone,
      name: options.name,
      avator: getStorageItem('avatar'),
    });
    const storageToken = getStorageItem('token');
    if (storageToken) {
      this.initData();
    } else {
      this.initDataThis = this.initData.bind(this);
      app.observe(app.globalData, 'tokenForAuth', {
        watchKey: 'tokenForAuth',
        watch: this.initDataThis,
      });
    }
  },
  onShow: function () {},
  onUnload: function () {
    if (this.initDataThis) {
      app.unObserve('tokenForAuth', this.initDataThis);
    }
  },
  handleBlur() {
    this.setData({
      isFocus: false,
    });
  },
  toComment() {
    this.setData({
      isFocus: true,
    });
  },
  initComments() {
    getComments({
      target_id: this.data.caseId,
    }).then((res) => {
      this.setData({
        commentList: res.data.list,
      });
    });
  },
  initCaseDetail() {
    getCaseDetail({
      caseId: this.data.caseId,
    })
      .then((res) => {
        this.setData({
          case_name: res.data.case_name,
          more_information_json: res.data.more_information_json,
        });
        if (res.data.case_picture_json.length > 0) {
          this.setData({
            imageList: JSON.parse(res.data.case_picture_json),
          });
        }
        if (res.data.case_video_json.length > 0) {
          this.setData({
            videoList: JSON.parse(res.data.case_video_json),
          });
        }
        this.setData({
          likes: res.data.likes,
          isLike: res.data.isLike === '1',
        });
      })
      .catch((err) => {});
  },
  sendComment() {
    if (!this.data.commentCt) {
      wx.showToast({
        title: '评论内容不能为空!',
        icon: 'none',
      });
      return;
    }
    saveComment({
      content: this.data.commentCt,
      target_id: this.data.caseId,
      userType: getStorageItem('hlrUserType'),
    })
      .then((res) => {
        wx.showToast({
          title: '评论成功!',
          icon: 'none',
        });
        this.setData({
          commentCt: '',
        });
        this.initComments();
      })
      .catch((err) => {});
  },
  handleCommentIpt(e) {
    this.setData({
      commentCt: e.detail.value,
    });
  },
  toggleLike() {
    if (this.data.isLike) {
      unpraise({
        target_id: this.data.caseId,
      }).then((res) => {
        this.setData({
          isLike: false,
          likes: this.data.likes - 1,
        });
        wx.showToast({
          title: '取消点赞成功!',
          icon: 'none',
        });
        app.globalData.needRefreshCaseList = '1';
      });
    } else {
      praise({
        target_id: this.data.caseId,
        userType: getStorageItem('hlrUserType'),
      }).then((res) => {
        this.setData({
          isLike: true,
          likes: this.data.likes + 1,
        });
        wx.showToast({
          title: '点赞成功!',
          icon: 'none',
        });
        app.globalData.needRefreshCaseList = '1';
      });
    }
  },
  callPhone() {
    if (!this.data.phone) {
      wx.showToast({
        title: '没有电话!',
        icon: 'none',
      });
    } else {
      wx.makePhoneCall({
        phoneNumber: this.data.phone,
        success: () => {},
      });
    }
  },
  toSchedule() {
    wx.navigateTo({
      url: '/pages/weddingMan/personal/schedules/checkSchedules/checkSchedules?userId=' + this.data.userId + '&name=' + this.data.name,
    });
  },
  updated() {
    this.setData({
      avator: getStorageItem('avatar'),
    });
    this.initData();
  },
  initData() {
    this.initCaseDetail();
    this.initComments();
  },
  previewVideo(e) {
    wx.navigateTo({
      url: '/pages/weddingMan/personal/videoPlay/videoPlay?videoPath=' + e.currentTarget.dataset.path,
    });
  },
});
